﻿/**
 * [1 Timothy 3:16-17 ESV]
 * All Scripture is breathed out by God and profitable
 * for teaching, for reproof, for correction, and for training in righteousness,
 * that the man of God may be complete, equipped for every good work.
 *
 * BibleReader is an simple application for you to study God's Word.
 * The main user of this application maybe the pastor who's main language
 * is Chinese Simplified.
 *
 * You can use it totally free, and if you want to get source code of
 * this application, please email me at dweng123@gmail to get the source
 * code.
 *
 */
#include <QPrinter>
#include <QPrintDialog>
#include "bibletextcomparebrowser.h"
#include "bibletextblockdata.h"
#include "Logger.h"

BibleTextCompareBrowser::BibleTextCompareBrowser(BibleReaderCore *brc, QWidget *parent):
    QTextBrowser(parent)
{
    brCore = brc;

    btFontFamily = brc->getConfigurator()->getBibleTextFontFamily();
    btFontSize = brc->getConfigurator()->getBibleTextFontSize();

    // set labels css class
    labels << "red" << "green" << "blue";

}

BibleTextCompareBrowser::~BibleTextCompareBrowser()
{

}



void BibleTextCompareBrowser::showComparedBibleText()
{
    QString bibleVersion;
    bibleVersion = brCore->getCurrentVersion();
    int currentBook = brCore->getCurrentBookNumber();
    int currentChapter = brCore->getCurrentChapterNumber();
    int currentVerse = brCore->getCurrentVerseNumber();
    QList<BibleBook> allBooks = brCore->getAllBooks();
    QString currentBookName = brCore->getCurrentBook(allBooks).getLongName();
    BibleChapter chapter = brCore->getChapter(bibleVersion, currentBook, currentChapter);
    QList<BibleInfo> allbibles = brCore->getAllBibleVersions();
    QList<BibleInfo> bibles;
    QSet<QString> biblesForComparing = brCore->getBiblesForComparing();
    LOG_INFO() << biblesForComparing.toList();
    // remove bible which is not contained in bibles for comparing
    for (int i = 0; i < allbibles.size(); i++) {
        if (biblesForComparing.contains(allbibles[i].getVersion())) {
            bibles.push_back(allbibles[i]);
        }
    }
    // add head title
    QString html = "";
    html.append("<html><head>");
    html.append(QString("</head><body>"));
    html.append("<h1 align='center'>").append(currentBookName).append(" ").append(QString::number(currentChapter)).append("</h1>");

    QList<BibleChapter> chapters;
    for (int i = 0; i < bibles.count(); i++) {
        chapters.push_back(brCore->getChapter(bibles[i].getVersion(), currentBook, currentChapter));
    }

    for (int j = 0; j < chapter.getVersesCount(); j++) {
        html.append(QString("<table border='2px'><th><td align='center' colspan='2'><span style='font-size:large;'><a name='v%1%2' href='#v%3%4'>%5:%6</a></span></td></th>")
                    .arg(currentChapter).arg(j+1).arg(currentChapter).arg(j+1).arg(currentChapter).arg(j+1));
        for (int i = 0; i < bibles.count(); i++) {
            QList<BibleVerse> verses = chapters[i].getVersesList();

            BibleVerse verse = verses.value(j);

            QString verseText = verse.getVerseText();
            verseText.replace("<", "&lt;");
            verseText.replace(">", "&gt;");

            verseText.replace("&lt;", "<sup>&lt;");
            verseText.replace("&gt;", "&gt;</sup>");

            html.append(QString("<tr><td><span style='color:%1; font-family:%6; font-size:large;'>%2</td><td><span style='font-size:large;font-family:%4'>%3</span></td></tr>")
                            .arg(labels[i%labels.size()]).arg(bibles[i].getFullname()).arg(verseText).arg(btFontFamily).arg(btFontFamily));

        }
        html.append("</table><hr />\n");
    }
    chapters.clear();
    setHtml(html.append("</body></html>"));
    //LOG_INFO() << html;
    QString anchorName = QString("v%1%2").arg(currentChapter).arg(currentVerse);
    LOG_INFO() << anchorName;
    scrollToAnchor(anchorName);
}

bool BibleTextCompareBrowser::printComparedText()
{
    QPrinter printer;

    QPrintDialog *dialog = new QPrintDialog(&printer, this);
    dialog->setWindowTitle(tr("Print Document"));
    if (this->textCursor().hasSelection())
       dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
    if (dialog->exec() != QDialog::Accepted)
       return false;

    this->print(&printer);
    return true;
}

